<template xmlns:el-col="http://www.w3.org/1999/html">
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="产品名称" prop="productName">
        <el-input
          v-model="queryParams.productName"
          placeholder="请输入产品名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品标识" prop="productKey">
        <el-input
          v-model="queryParams.productKey"
          placeholder="请输入产品标识"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品状态" prop="enableFlag">
        <el-select v-model="queryParams.enableFlag" placeholder="请选择产品状态" style="width: 200px" clearable>
          <el-option label="开启" value="1" />
          <el-option label="关闭" value="0" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['manager:productInfo:add']"
        >新增</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-row>
      <el-col style="padding: 8px" :span="6" v-for="(item,index) in productInfoList" :key="index">
        <el-card style="max-width: 100%">
          <template #header>
            <div class="card-header" style="font-size: 25px;display: flex;align-items: center; ">
              <svg-icon icon-class="cube" style="width: 1em; height: 1em; margin-right: 8px" /> {{ item.productName }}
            </div>
          </template>
          <el-row>
            <el-col :span="18">
              <el-form label-width="78px">
                <el-form-item label="产品标识:" prop="productKey" style="margin-bottom: 0">
                  <span>{{ item.productKey }}</span>
                </el-form-item>
                <el-form-item label="产品状态:" prop="enableFlag" style="margin-bottom: 0">
                  <el-tag v-if="item.enableFlag === 1" type="success">开启</el-tag>
                  <el-tag v-else-if="item.enableFlag === 0" type="danger">关闭</el-tag>
                  <el-tag v-else type="info">未知</el-tag>
                </el-form-item>
                <el-form-item label="创建人:" prop="createBy" style="margin-bottom: 0">
                  <span>{{ item.createBy }}</span>
                </el-form-item>
                <el-form-item label="创建时间:" prop="createTime" style="margin-bottom: 0">
                  <span>{{ item.createTime }}</span>
                </el-form-item>
              </el-form>
            </el-col>
            <el-col :span="6" style="display:flex;align-items: center">
              <img style="width: 100px;height: 70px" :src="item.productPicture ? baseUrl+item.productPicture : defaultImg" alt="" />
            </el-col>
          </el-row>
          <template #footer>
            <el-row :gutter="15">
              <el-col :span="8">
                <el-button class="cu-btn" type="primary" icon="EditPen" v-hasPermi="['manager:productInfo:edit']" plain @click="handleUpdate(item)">编辑</el-button>
              </el-col>
              <el-col :span="8">
                <el-button class="cu-btn" type="warning" icon="View" v-hasPermi="['manager:productInfo:detail']" plain @click="handleView(item)">详情</el-button>
              </el-col>
              <el-col :span="8">
                <el-button type="danger" icon="Delete" v-hasPermi="['manager:productInfo:remove']" @click="handleDelete(item)" plain>删除</el-button>
              </el-col>
            </el-row>
          </template>
        </el-card>
      </el-col>
    </el-row>

    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改产品信息表对话框 -->
    <el-dialog :title="title" v-model="open" width="800px" append-to-body>
      <el-form ref="productInfoRef" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="产品名称" prop="productName">
              <el-input v-model="form.productName" placeholder="请输入产品名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="产品标识" prop="productKey">
              <el-input v-model="form.productKey" placeholder="请输入产品标识" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item label="产品配置" prop="productConfig">
          <el-input v-model="form.productConfig" readonly placeholder="请对产品属性进行配置">
            <template #append>
              <el-button icon="Setting" @click="configProduct(form)" />
            </template>
          </el-input>
        </el-form-item>

        <el-form-item label="产品链接" prop="productLink">
          <el-input v-model="form.productLink" placeholder="请输入产品链接" />
        </el-form-item>
        <el-form-item label="产品状态" prop="enableFlag">
          <el-radio-group v-model="form.enableFlag">
            <el-radio :label="1">开启</el-radio>
            <el-radio :label="0">关闭</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="产品图片" prop="productPicture">
          <image-upload v-model="form.productPicture" :limit="1" :fileSize="10"></image-upload>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

    <product-config :open="openConfig"
                    :productId="productId"
                    :config="config"
                    @setConfig="setConfig"
                    @closeConfig="closeConfig">
    </product-config>
  </div>
</template>

<script setup name="ProductInfo">
import {getProductInfo, listProductInfo,updateProductInfo,addProductInfo,delProductInfo } from "@/api/manager/productInfo";
import ProductConfig from './config'
import defaultImg from '@/assets/images/pic_device.png'
const baseUrl = import.meta.env.VITE_APP_BASE_API;

const { proxy } = getCurrentInstance();
const router = useRouter();

const productInfoList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const openConfig = ref(false)
const productId = ref(0)
const config = ref('')

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 12,
    productName: null,
    productKey: null,
    productConfig: null,
    productPicture: null,
    productLink: null,
    enableFlag: null,
    deleted: null,
  },
  rules: {
    productName: [
      { required: true, message: "产品名称不能为空", trigger: "blur" }
    ],
    productKey: [
      { required: true, message: "产品标识不能为空", trigger: "blur" }
    ],
    productConfig: [
      { required: true, message: "产品配置json内容不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询产品信息表列表 */
function getList() {
  loading.value = true;
  listProductInfo(queryParams.value).then(response => {
    productInfoList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    productName: null,
    productKey: null,
    productConfig: null,
    productPicture: null,
    productLink: null,
    enableFlag: 1,
    deleted: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    remark: null
  };
  proxy.resetForm("productInfoRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加产品信息表";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getProductInfo(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改产品信息表";
  });
}

function handleView(item){
  proxy.$modal.msgSuccess("暂未开发，敬请期待");
}
function openObjectModel(item){
  proxy.$modal.msgSuccess("暂未开发，敬请期待");
}

function configProduct(item){
  //proxy.$modal.msgSuccess("暂未开发，敬请期待");
  openConfig.value = true
  productId.value = item.id
  config.value = item.productConfig
}

function setConfig(childConfig){
  form.value.productConfig = childConfig
  closeConfig()
}

function closeConfig(){
  openConfig.value = false
  productId.value = 0
  config.value = ''
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["productInfoRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateProductInfo(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addProductInfo(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除产品信息表编号为"' + _ids + '"的数据项？').then(function() {
    return delProductInfo(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('manager/productInfo/export', {
    ...queryParams.value
  }, `productInfo_${new Date().getTime()}.xlsx`)
}

getList();
</script>
<style lang="scss" scoped>

</style>
